/**
 * All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
 * <p>
 * MmsDeptProvinceCityMatchInfoService
 * 部门地市信息表
 * <p>
 * 修改纪录
 * 2022-02-22 版本：1.0 xinfushuo 创建。
 *
 * @version 版本：1.0
 * @author 作者：xinfushuo
 * 创建日期：2022-02-22
 */
package com.topscomm.mms.service.impl;

import com.topscomm.cbo.model.TreeModel;
import com.topscomm.mms.MmsSystemConst;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.common.DatacodeCache;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mms.service.IMmsDeptProvinceCityMatchInfoService;
import com.topscomm.mms.service.impl.auto.MmsDeptProvinceCityMatchInfoServiceAuto;

import javax.swing.text.TabableView;
import java.util.*;
import java.util.stream.Collectors;

@Service(value = "mmsDeptProvinceCityMatchInfoService")
@Scope(value = "singleton")
public class MmsDeptProvinceCityMatchInfoService extends MmsDeptProvinceCityMatchInfoServiceAuto implements IMmsDeptProvinceCityMatchInfoService {
    private final DatacodeCache datacodeCache;
    private final MmsProvinceCityMatchinfoService mmsProvinceCityMatchinfoService;

    public MmsDeptProvinceCityMatchInfoService(DatacodeCache datacodeCache, MmsProvinceCityMatchinfoService mmsProvinceCityMatchinfoService) {
        this.datacodeCache = datacodeCache;
        this.mmsProvinceCityMatchinfoService = mmsProvinceCityMatchinfoService;
    }

    /**
     * @description: // 构造树 根据编码配置获取对应范围
     * @author xinfushuo
     * @param
     * @return
     * @date 2022/2/24
     * @modify
     */
    @Override
    public List<TreeModel> queryTreeListForDeptProvince(String depcommoncodeOfUser) {
        List<Map<String, Object>> listModule = new ArrayList<>();
        StringBufferProxy sql = new StringBufferProxy();
        Map<String, Object> paramMap = new HashMap<>();
        //如果是运营人员，返回所有一级部门
        if (checkIsOperators() || getSessionUserBean().getUser().getAdminlevel() == 9 ) {
            listModule = mmsProvinceCityMatchinfoService.queryMap(paramMap, "queryForDeptProvinceMatch");
        } else if (checkIsNotProvince()) {
            //如果用户所在部门不是省区 而是国网南网等部门
            String ranges = getRangeFromDeptCommoncode(depcommoncodeOfUser);
             if (!ranges.isEmpty()) {
                //从 "1,2,3,4" 转换为 "'1','2','3','4'"
                ranges = Arrays.stream(ranges.split(",")).map(s -> "'".concat(s).concat("'")).collect(Collectors.joining(","));
                paramMap.put("deptcommoncodes", ranges);
                listModule = mmsProvinceCityMatchinfoService.queryMap(paramMap, "queryForDeptProvinceMatch");
            }
        }
        List<Map<String, Object>> listNewModule = new ArrayList<Map<String, Object>>();
        listNewModule.addAll(listModule);
        TreeModel treeModel = new TreeModel();
        List<TreeModel> treeList = treeModel.buildTree(listNewModule, true, "全国");
        return treeList;
    }

    /**
     * @description: 检查当前用户是否为国网南网等特殊部门
     * @author xinfushuo
     * @date 2022/2/24
     * @modify
     */
    public Boolean checkIsNotProvince() {
        String deptcommoncode = this.getDeptCommonCode(getSessionUserBean().getDept().getCode());
        if(deptcommoncode == null ){
            return false;
        }
        List<Map<String, Object>> rangelist = datacodeCache.getCodeListMap(MmsSystemConst.DeptcommonProvinceViewRange, "", "sortcode");
        HashMap<String, Object> rangeCode = new HashMap<>();
        rangeCode.putAll(
                rangelist.stream().filter(range -> {
                    return ConvertUtil.convertToString(range.get("code")).contentEquals(deptcommoncode);
                }).findAny().orElse(new HashMap<>())
        );
        return rangeCode.isEmpty() ? false : true;
    }

    /**
     * @description:
     * @author xinfushuo
     * @param
     * @throws
     * @return
     * @date 2022/2/24
     * @modify
     */
    public String getRangeFromDeptCommoncode(String deptcommoncode) {
        List<Map<String, Object>> rangelist = datacodeCache.getCodeListMap(MmsSystemConst.DeptcommonProvinceViewRange, "", "sortcode");
        List<Map<String, Object>> deptcommoncode1List = datacodeCache.getCodeListMap("DeptCommonCodeTrack", "", "sortcode");
        Map<String, String> rangeMap = rangelist.stream().collect(Collectors.toMap(map -> ConvertUtil.convertToString(map.get("code")), map -> ConvertUtil.convertToString(map.get("name"))));

        String ranges = rangeMap.get(deptcommoncode);
        ranges = ranges.replaceAll("\\s+", "");
        if (ranges.contentEquals(MmsSystemConst.DeptcommonProvinceViewAll)) {
            ranges = deptcommoncode1List.stream().filter(dept->{
               return  !rangeMap.containsKey(ConvertUtil.convertToString(dept.get("code")));
            }).map(map->ConvertUtil.convertToString(map.get("code"))).collect(Collectors.joining(","));
            return ranges;
        } else {
            return ranges;
        }
    }

    /**
     * @description:
     * @author xinfushuo
     * @param
     * @throws
     * @return
     * @date 2022/2/24
     * @modify
     */
    public String getDeptCodesFromDeptcommonProvinceViewRange() {
        List<Map<String, Object>> rangelist = datacodeCache.getCodeListMap(MmsSystemConst.DeptcommonProvinceViewRange, "", "sortcode");
        String deptcodes = rangelist.stream().map(s -> "'".concat(ConvertUtil.convertToString(s.get("code"))).concat("'")).collect(Collectors.joining(","));
        return deptcodes;
    }
}
